﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Shapes;

namespace AIStudio.Wpf.TileManager
{
	public class Tile : HeaderedContentControl
	{
        private Thumb _dragThumb_V;
        private Thumb _dragThumb_H;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _dragThumb_V = GetTemplateChild("PART_DragThumb_V") as Thumb;
            _dragThumb_V.DragDelta += _dragThumb_V_DragDelta;
            _dragThumb_H = GetTemplateChild("PART_DragThumb_H") as Thumb;
            _dragThumb_H.DragDelta += _dragThumb_H_DragDelta;
        }

        void _dragThumb_V_DragDelta(object sender, DragDeltaEventArgs e)
        {
            if (TileView != null)
            {
                if (TileView.MaximizedTileLocation == Location.Top)
                {
                    TileView.MaximizedRatio += e.VerticalChange / TileView.ActualHeight;
                }
                else if (TileView.MaximizedTileLocation == Location.Buttom)
                {
                    TileView.MaximizedRatio -= e.VerticalChange / TileView.ActualHeight;
                }
                if (TileView.MaximizedRatio < 0.1)
                {
                    TileView.MaximizedRatio = 0.1;
                }
                else if (TileView.MaximizedRatio > 0.9)
                {
                    TileView.MaximizedRatio = 0.9;
                }
                TileView.InvalidateArrange();
                TileView.InvalidateMeasure();
            }
        }

        void _dragThumb_H_DragDelta(object sender, DragDeltaEventArgs e)
        {
            if (TileView != null)
            {
                if (TileView.MaximizedTileLocation == Location.Left)
                {
                    TileView.MaximizedRatio += e.HorizontalChange / TileView.ActualWidth;                   
                }
                else if (TileView.MaximizedTileLocation == Location.Right)
                {
                    TileView.MaximizedRatio -= e.HorizontalChange / TileView.ActualWidth;
                }
                if (TileView.MaximizedRatio < 0.1)
                {
                    TileView.MaximizedRatio = 0.1;
                }
                else if (TileView.MaximizedRatio > 0.9)
                {
                    TileView.MaximizedRatio = 0.9;
                }
                TileView.InvalidateArrange();
                TileView.InvalidateMeasure();
            }
        }

        public TileView TileView { get; set; }

        #region Ctor

        static Tile()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(Tile), new FrameworkPropertyMetadata(typeof(Tile)));
		}

		#endregion

		#region Properties

		internal int LayoutOrder
		{
			get;
			set;
		}

		#endregion

		#region Overrides

		protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
		{
			base.OnMouseDown(e);

			if (Activated != null)
				Activated(this, new RoutedEventArgs());
		}

		#endregion

		#region Events

		public event RoutedEventHandler Activated;

		#endregion
	}
}
